#ifndef __STATUSLISTENER_HPP__
#define __STATUSLISTENER_HPP__

/**
 *  This file is part of PennerBot
 *
 *  Copyright (C) 2010 microkernel
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <queue>

#include <boost/thread/recursive_mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/shared_ptr.hpp>

#include <boost/variant.hpp>
#include <boost/tuple/tuple.hpp>

#include <core/config.hpp>
#include <core/messages.hpp>

namespace PBot
{
namespace GUI
{

class MainFrame;

typedef boost::variant<unsigned int, std::string> pvariant;

class StatusListener
{
private:
    StatusListener();
    StatusListener(MainFrame* _frame);
    StatusListener(const StatusListener&);

public:
    ~StatusListener();

    static StatusListener& GetInstance();
    static void Destroy();

    void SetFrame(MainFrame* frame);

    void Initialize(const std::vector<Core::Config*>&);

    void StartListener();

    void Dispatch(unsigned int id, Messages status, pvariant v = 0);

    unsigned int NumMessages();
    void RequestStop();

private:
    void Listen();

    static StatusListener* instance;

    typedef boost::tuple<unsigned int, Messages, pvariant>  message;

    std::queue<message> messages;

    boost::recursive_mutex mutex;
    boost::recursive_mutex io_mutex;

    MainFrame *frame;

    volatile bool stopRequested;

    typedef boost::shared_ptr<boost::thread> bthread;
    bthread thread;

};

}
}


#endif // __STATUSLISTENER_HPP__
